package order

import (
	"booking/common/handler"
	"booking/common/validator"
	"booking/models"
	"github.com/gin-gonic/gin"
)

func Cancel(c *gin.Context) {
	var params struct {
		OrderNo string `json:"order_no" binding:"required"`
	}

	if err := c.ShouldBindJSON(&params); err != nil {
		handler.Error(validator.Translate(err), c)
		return
	}
	var order models.Order
	models.Db.Model(models.Order{}).
		Where(map[string]any{
			"order_no": params.OrderNo,
		}).
		First(&order)
	if order.Id == 0 {
		handler.Error("订单不存在", c)
		return
	}
	models.Db.Model(models.Order{}).Where(map[string]any{
		"order_no": params.OrderNo,
	}).Update("status", models.OrderStatusCanceled)

	handler.Success(map[string]any{
		"order_no": order.OrderNo,
	}, c)

}
